package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.AlarmPushDto;
import com.hualude.roadsign.dto.HighLeverDto;
import com.hualude.roadsign.dto.DTRequestDto;
import com.hualude.roadsign.dto.DTResponseDto;
import com.hualude.roadsign.model.HighLeverEntity;
import com.hualude.roadsign.repository.AlarmPushRepository;
import com.hualude.roadsign.repository.AlarmRepository;
import com.hualude.roadsign.repository.HighLeverFaultRepository;
import com.hualude.roadsign.repository.HighLeverRepository;
import com.hualude.roadsign.service.AlarmService;
import com.hualude.roadsign.service.HighLeverManageService;
import com.hualude.roadsign.utils.FormatNumber;
import com.hualude.roadsign.utils.UdpSender;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xu_jiaHui on 2018/3/24.
 */
@Service
public class HighLeverManageServiceImpl implements HighLeverManageService {

    @Resource
    private HighLeverRepository highLeverRepository;
    @Resource
    private HighLeverFaultRepository highLeverFaultRepository;
    @Resource
    private AlarmRepository alarmRepository;
    @Resource
    private AlarmPushRepository alarmPushRepository;

    @Override
    public DTResponseDto getHighLever4DT(DTRequestDto dtRequestDto,
                                         String actorIds,
                                         Integer orgId,
                                         Boolean audit,
                                         String startRoad,
                                         String endRoad,
                                         String roadName,
                                         Integer alarmBinding) throws Exception {
        List<HighLeverDto> HighLeverDtos = highLeverRepository.getHighLever4DT(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName, alarmBinding);
        Integer count = highLeverRepository.getHighLever4DTCount(
                dtRequestDto, actorIds, orgId, audit, startRoad, endRoad, roadName, alarmBinding);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.draw, count, count, HighLeverDtos);
        return dtResponseDto;
    }


    @Override
    @Transactional
    public Integer deleteHighLever(String highLeverIds) throws Exception {
        String[] highLeverIdArray = highLeverIds.split(",");
        for (String highLeverId : highLeverIdArray) {
            highLeverFaultRepository.deleteHighLeverFault(Integer.parseInt(highLeverId));
            highLeverRepository.deleteHighLever(Integer.parseInt(highLeverId));
        }
        return highLeverIdArray.length;
    }

    @Override
    public Integer addHighLever(HighLeverDto highLeverDto) throws Exception {
        highLeverDto.setHighLeverLongitude(FormatNumber.resize(highLeverDto.getHighLeverLongitude()));
        highLeverDto.setHighLeverLatitude(FormatNumber.resize(highLeverDto.getHighLeverLatitude()));
        return highLeverRepository.save(highLeverDto.toBean()).getHighLeverId();
    }

    @Override
    @Transactional
    public Integer updateHighLever(HighLeverDto highLeverDto) throws Exception {
        return highLeverRepository.updateHighLever(
                highLeverDto.getHighLeverId(),
                highLeverDto.getProjectNumber(),
                highLeverDto.getHighLeverLongitude(),
                highLeverDto.getHighLeverLatitude(),
                highLeverDto.getHighLeverProvince(),
                highLeverDto.getHighLeverCity(),
                highLeverDto.getHighLeverArea(),
                highLeverDto.getHighLeverRoadName(),
                highLeverDto.getHighLeverStartRoad(),
                highLeverDto.getHighLeverEndRoad(),
                highLeverDto.getHighLeverSize(),
                highLeverDto.getHighLeverPhoto(),
                highLeverDto.getHighLeverInstallTime(),
                highLeverDto.getHighLeverInstallOrg(),
                highLeverDto.getHighLeverPeriod(),
                highLeverDto.getHighLeverServiceLife(),
                highLeverDto.getHighLeverRemark(),
                highLeverDto.getHighLeverCreatorId(),
                highLeverDto.getHighLeverModifierId(),
                highLeverDto.getHighLeverOrgId(),
                highLeverDto.getHighLeverMainOrgId());
    }

    @Override
    public List<String> getStartRoadByRoadName(String roadName, String actorIds, Integer orgId) throws Exception {
        return highLeverRepository.getStartRoadByRoadName(roadName, actorIds, orgId);
    }

    @Override
    public List<String> getEndRoadByStartRoad(String city, String highLeverRoadName, String startRoad, String actorIds, Integer orgId) throws Exception {
        return highLeverRepository.getEndRoadByStartRoad(city, highLeverRoadName, startRoad, actorIds, orgId);
    }

    @Override
    public List<HighLeverDto> getHighLever4APP(String actorIds,
                                               Integer orgId,
                                               Integer mCount,
                                               Integer mSize,
                                               String city,
                                               String startRoad,
                                               String endRoad,
                                               String roadName,
                                               String alarmType) throws Exception {
        return highLeverRepository.getHighLever4APP(actorIds, orgId, mCount, mSize, city, startRoad, endRoad, roadName, alarmType);
    }

    @Override
    public HighLeverDto getHighLeverById(Integer highLeverId) throws Exception {
        return highLeverRepository.getHighLeverById(highLeverId).get(0);
    }

    @Override
    public List<String> getCity4HighLever4App(String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return highLeverRepository.getCity4HighLever4App(actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getRoadName4HighLeverByCity(String actorIds, Integer orgId, String city, Integer mCount, Integer mSize) throws Exception {
        return highLeverRepository.getRoadName4HighLeverByCity(actorIds, orgId, city, mCount, mSize);
    }

    @Override
    public List<String> getEndRoadByStartRoad4App(String city, String highLeverRoadName, String startRoad, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return highLeverRepository.getEndRoadByStartRoad4App(city, highLeverRoadName, startRoad, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<String> getStartRoadByRoadName4App(String city, String roadName, String actorIds, Integer orgId, Integer mCount, Integer mSize) throws Exception {
        return highLeverRepository.getStartRoadByRoadName4App(city, roadName, actorIds, orgId, mCount, mSize);
    }

    @Override
    public List<HighLeverDto> getHighLever4Map4App(Integer orgId,
                                                   String actorIds,
                                                   double neLongitude,
                                                   double neLatitude,
                                                   double swLongitude,
                                                   double swLatitude) throws Exception {
        return highLeverRepository.getHighLever4Map4App(orgId, actorIds, neLongitude, neLatitude, swLongitude, swLatitude);
    }


    @Override
    @Transactional
    public Integer auditHighLever(String highLeverIds) throws Exception {
        String[] highLeverIdArray = highLeverIds.split(",");
        for (String highLeverId : highLeverIdArray) {
            highLeverRepository.auditHighLever(Integer.parseInt(highLeverId));
        }
        return highLeverIdArray.length;
    }

    @Override
    @Transactional
    public Integer setUpPatrolRecord(Integer highLeverId, String highLeverPatrolTime, String highLeverPatrolContent, Integer highLeverModifierId) throws Exception {
        return highLeverRepository.setUpPatrolRecord(highLeverId, highLeverPatrolTime, highLeverPatrolContent, highLeverModifierId);
    }

    @Override
    @Transactional
    public Integer bindingDevice(String deviceId, Integer highLeverId) throws Exception {
        HighLeverEntity entity = highLeverRepository.findOne(highLeverId);
        StringBuffer position = new StringBuffer();
        position.append(entity.getHighLeverProvince());
        position.append(entity.getHighLeverCity());
        position.append(entity.getHighLeverArea());
        position.append(" ");
        position.append(entity.getHighLeverRoadName());
        position.append(" ");
        position.append(entity.getHighLeverStartRoad());
        position.append("-");
        position.append(entity.getHighLeverEndRoad());
        int count1 = highLeverRepository.bindingDevice(deviceId, highLeverId);
        count1 += alarmRepository.bindingDevice(5, highLeverId, position.toString(), deviceId);

        UdpSender sender = UdpSender.getInstance();
        sender.sendMsg(deviceId + ",UPDATEALARM");

        return count1;
    }

    @Override
    @Transactional
    public Integer unbindingDevice(String deviceId, Integer highLeverId) throws Exception {
        int count = highLeverRepository.unbindingDevice(highLeverId);
        count += alarmRepository.unbindingDevice(deviceId);


        UdpSender sender = UdpSender.getInstance();
        sender.sendMsg(deviceId + ",UPDATEALARM");

        return count;

    }

    @Override
    public DTResponseDto showAlarmHistory(DTRequestDto dtRequestDto, String deviceId, Integer deviceType) throws Exception {
        List<AlarmPushDto> dtos = alarmPushRepository.getAlarmHistory4DT(dtRequestDto, deviceId, deviceType);
        int count = alarmPushRepository.getAlarmHistoryCount4DT(dtRequestDto, deviceId, deviceType);
        return new DTResponseDto(dtRequestDto.getDraw(), count, count, dtos);
    }

    @Override
    public List<HighLeverDto> getDeviceInfo(Integer deviceId) throws Exception {
        return highLeverRepository.getDeviceInfo(deviceId);
    }



}
